package com.wan.tool;

import android.content.Context;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * Created by wan on 13-12-28.
 */
public class CommonUtils {

    public static boolean checkWifiConnection2(Context context) {
        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);


        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifi != null && wifi.isAvailable() && wifi.isConnectedOrConnecting()) {
            Log.i("wifi", "wifi status:" + wifi.isAvailable() + ":" + wifi.isConnectedOrConnecting() + ":" + wifi.isConnected() + ":");
            return true;
        }
        return false;
    }

    public static boolean isWiFiActive(Context inContext) {
        Context context = inContext.getApplicationContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static void setMusicVolume(Context context, boolean up) {
        int maxMusicVolum = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int currentMusicVolun = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getStreamVolume(AudioManager.STREAM_MUSIC);
        if (up) {
            currentMusicVolun += maxMusicVolum / 10;
        } else {
            currentMusicVolun -= maxMusicVolum / 10;
        }
        if (currentMusicVolun > maxMusicVolum) {
            currentMusicVolun = maxMusicVolum;
        }
        if (currentMusicVolun < 0) {
            currentMusicVolun = 0;
        }
        ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setStreamVolume(AudioManager.STREAM_MUSIC, currentMusicVolun, 0);
    }


}
